package com.example.demo.config;

import com.example.demo.mapper.AdminMapper;
import com.example.demo.model.LoginUser;
import com.example.demo.model.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Mr.Xi
 * Date: 2023-04-24
 * Time: 21:01
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        // 从数据库中查询 用户 信息
        LoginUser user = adminMapper.getLoginUserInfoByPhoneForLogin(phone);
        if(user == null){
            throw new RuntimeException("没有该用户!");
        }
        // 查询用户的权限信息，并封装到 LoginUser 中
        return new TokenUser(user,null);
    }
}
